Memory management relates to managing memory resources in a computer system. This management may include allocating memory used by programs running on the computer system as well as reclaiming memory that had been allocated to the programs but is no longer used by the programs. In many programming environments, memory allocated to a program is often allocated from a heap. A heap, as used herein, relates to an area of memory that may be used for dynamic memory allocation.
In some programming languages, management of the heap is the programmer's responsibility. This approach to memory management is often termed manual or explicit memory management. The complexity of manual memory management may lead to many common errors that may cause unexpected or erroneous program behavior and crashes. As a result, a large portion of developer time may be spent debugging and trying to correct such errors.
One common error that may occur when using manual memory management is space leaks. Space leaks occur when allocated memory is no longer referenced but is not released. For example, if a program only frees memory space for the first element of linked list, the remaining list elements may no longer be reachable by the program and consequently may neither be used nor recovered. If enough memory leakage occurs, the heap may become saturated with unreferenced and unusable memory.
Another common error results from using an address to reference memory that has already been manually released. Such “use-after-free errors” (sometimes referred to as “stale pointer errors”) do not occur in systems that use automatic memory management.
An alternative approach to manual memory management is automatic memory management. Automatic memory management relates to a technique for managing memory that often includes provisions for automatically reclaiming memory that is “unreachable” by software (e.g., programs) running on a system. Automatic memory management may be implemented as a service that is part of a programming language (e.g., Java®) or an extension to the language.
Garbage collection is a form of automatic memory management that relates to automatically finding and reclaiming unreachable memory. Garbage collection is widely utilized by many modern object-oriented programming languages. Garbage collection prevents memory loss due to, for example, space leaks, by automatically reclaiming objects that are unreachable.
Garbage collection is often implemented in the form of a garbage collector. A garbage collector is responsible for, inter alia, reclaiming areas of memory that are considered to be unreachable. These areas are often said to be “dead” or “garbage” and are considered eligible for reclaiming by the garbage collector. Areas in memory that are reachable are often said to be “live”. These areas may include memory that is allocated and actively used by the software. Live areas are not collected by the garbage collector.
It is also common for garbage collection to perform other types of memory management such as memory re-organization or “defragmentation”. During normal operation, objects are dynamically created and deleted in the heap, eventually causing gaps of unused memory between objects, also known as fragmentation. Fragmentation can adversely affect memory system performance, especially where large chunks of data have to be stored across several gaps of unused memory in the heap. During memory re-organization, a garbage collector compacts objects that are located throughout the heap into a substantially contiguous section of memory. This object compaction (e.g., defragmentation) removes the fragmented gaps of unused memory and, consequently, creates larger portions of contiguous unused memory enabling increased memory system performance.